package 链表;

/**
 * @author 陈艺博
 * @date 2021-02-18 09:13
 * @description https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 **/
public class _83__remove_duplicates_from_sorted_list_删除排序链表中的重复元素 {
    // 递归
    /*public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        head.next = deleteDuplicates(head.next);
        if (head.val == head.next.val) {
            head = head.next;
        }
        return head;
    }*/

    //迭代
    public ListNode deleteDuplicates(ListNode head) {
        if (null == head || head.next == null) {
            return head;
        }

        ListNode temp = head;
        while (null != temp.next) {
            if (temp.val == temp.next.val) {
                //如果删除了节点，则保持当前头结点不变，判断新的下一个节点值是否与当前头结点值相等。
                temp.next = temp.next.next;
            } else {
                //如果未删除节点，则将头结点移动到下一个节点
                temp = temp.next;
            }
        }
        return head;
    }
}
